package com.seg.web.controller.wms;

import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.system.domain.TSparePart;
import com.seg.system.service.ITSparePartService;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/wms/zebra")
public class ZBraPrinterController extends BaseController {
    @Autowired
    private ITSparePartService tSparePartService;

    @PostMapping("/{ids}")
    public AjaxResult printerMaterialNos(@PathVariable Long[] ids) {
        // 批量获取物料信息 //TODO
        List<TSparePart> list = tSparePartService.getPartByIds(ids);
        if (list == null || list.isEmpty()) {
            return error("备件信息不存在");
        }
        for (TSparePart tSparePart : list) {
            if (tSparePart.getSpecification().length() > 20) {
                tSparePart.setSpecification(tSparePart.getSpecification().substring(0, 20));
            }
            if (tSparePart.getName().length() > 15) {
                tSparePart.setName(tSparePart.getName().substring(0, 15));
            }
            if (tSparePart.getBrand().length() > 20) {
                tSparePart.setBrand(tSparePart.getBrand().substring(0, 20));
            }
        }
        return success(list);
    }

//    @DeleteMapping("/{ids}")
//    public AjaxResult getPartByMaterialNo(@PathVariable Long[] materialNos) {
//        //批量获取物料信息 //TODO
////        TSparePart t = tSparePartService.getPartByMaterialNos(materialNos);
////        if (t == null) {
////            return error("备件信息不存在");
////        }
//        try {
//            String ip = "192.168.1.44";
//            Integer port = 8095;
//            Connection printerConnection = getConnection(1, ip, port);
//            printerConnection.open();
//            ZebraPrinter printer = ZebraPrinterFactory.getInstance(printerConnection);
////            printer.print();
////            printer.printStoredFormat();
//            printerConnection.close();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//        return null;
//    }

    /**
     * 0 tcp 1  driver  2 usb
     *
     * @param type
     * @return
     * @throws ConnectionException
     */
    public Connection getConnection(Integer type, String ip, Integer port) throws ConnectionException {
        if (type == 0) {
            try {
                return new TcpConnection(ip, port);
            } catch (NumberFormatException e) {
                throw new ConnectionException(e.getMessage());
            }
        }
//        } else if (type == 1) {
//            DiscoveredPrinterDriver printer = (DiscoveredPrinterDriver) usbPrinterList.getSelectedItem();
//            return new DriverPrinterConnection(printer.printerName);
//        } else if (type == 2) {
//            return new UsbConnection(usbDirectAddressTextField.getText());
//        }
        return null;
    }
}
